let code = require('../../../consts/consts');
module.exports = function(app){
    return new Handler(app);
}
let Handler = function(app){
    this.app = app;
    this.roomMgr = this.app.get('roomMgr');
    this.redis_db = this.app.get('redis_db');
};
Handler.prototype.outCard = function(msg,session,next){
    let suid = session.get('uid');
    let stid = session.get('tid');
    if(!suid  || !stid  ){
        return next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    };
    let card = msg.card;
    if(!card){
        return next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
    let table = this.roomMgr.getTable(stid);
    if(table){
        let res = table.outCard({
            uid:suid,
            card:card
        });
        next(null,res);
    }else
    {
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
};
Handler.prototype.ready = function(msg,session,next){
    let suid = session.get('uid');
    let stid = session.get('tid');
    if(!suid || !stid ){
        return next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    };
    let table = this.roomMgr.getTable(stid);
    if(table){
        let res = table.gameReady(suid);
        next(null,res);
    }else
    {
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
};

//吃  0 左吃  1 中吃   2 右吃   3 碰  4 明杠杠 5 暗杠  6 碰杠  7 胡 8 自摸 9 过
Handler.prototype.operate = function(msg,session,next){
    let uid = session.get('uid');
    let tid = session.get('tid');
    let type = msg.type;
    if(!type){
        return next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
    let table = this.roomMgr.getTable(tid);
    if(table){
        let res = table.operate({
            uid:uid,
            type:type
        });
        next(null,res);
    }else
    {
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
};
Handler.prototype.gameExit = async function(msg,session,next){
    let suid = session.get('uid');
    let stid = session.get('tid');
    if(!suid || !stid ){
        return next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    };
    let table = this.roomMgr.getTable(stid);
    if(table){
        let res = table.gameExit({
            reason:code.EXIT_REASON.PLAYER_EXIT,
            uid:suid
        });
        if(res.code != code.FAIL){
            let serverId = session.frontendId;
            await this.redis_db.HMSETFieldAsync(suid,'serverId',serverId);
        }
        next(null,res);
    }else
    {
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
}